import apiClient from '@/api/axios.js';

export class ChatSessionApi {

  // 查询所有聊天会话
  static async getAllChatSessions() {
    try {
      const response = await apiClient.get('/chatsession');
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 chatSessionID 查询聊天会话
  static async getChatSessionById(chatSessionID) {
    try {
      const response = await apiClient.get(`/chatsession/${chatSessionID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 user1ID 和 user2ID 查询聊天会话
  static async getChatSessionByUserIds(user1ID, user2ID) {
    try {
      const response = await apiClient.get(`/chatsession/user1/${user1ID}/user2/${user2ID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 新增聊天会话
  static async createChatSession(params) {
    const { user1ID, user2ID, creationTime } = params;
    try {
      const response = await apiClient.post('/chatsession', {
        user1ID,
        user2ID,
        creationTime
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 更新聊天会话
  static async updateChatSession(chatSessionID, params) {
    const { user1ID, user2ID, creationTime } = params;
    try {
      const response = await apiClient.put(`/chatsession/${chatSessionID}`, {
        chatSessionID,
        user1ID,
        user2ID,
        creationTime
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 删除聊天会话
  static async deleteChatSession(chatSessionID) {
    try {
      const response = await apiClient.delete(`/chatsession/${chatSessionID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }
}
